package cn.broadsense.biz.service.impl;

import cn.broadsense.biz.domain.TagType;
import cn.broadsense.biz.dto.RuleTreeDto;
import cn.broadsense.biz.query.RuleQuery;
import cn.broadsense.biz.utils.IdGenerationUtil;
import cn.broadsense.biz.vo.TagRuleVo;
import cn.broadsense.common.exception.ServiceException;
import cn.broadsense.common.utils.TreeUtils;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.broadsense.biz.domain.TagRule;
import cn.broadsense.biz.service.TagRuleService;
import cn.broadsense.biz.mapper.TagRuleMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author bs
 * @description 针对表【t_tag_rule(标签规则)】的数据库操作Service实现
 * @createDate 2023-09-28 10:10:55
 */
@Service
public class TagRuleServiceImpl extends ServiceImpl<TagRuleMapper, TagRule>
        implements TagRuleService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertRule(TagRuleVo ruleVo) {
        String ruleCode = IdGenerationUtil.ruleCode();
        TagRule tagRule = new TagRule();
        tagRule.setRuleCode(ruleCode);
        tagRule.setRuleName(ruleVo.getRuleName());
        tagRule.setRuleDescribe(ruleVo.getRuleDescribe());
        if (ruleVo.getStatus() != null) {
            tagRule.setStatus(ruleVo.getStatus());
        }
        if (ruleVo.getShowMap() != null) {
            tagRule.setShowMap(ruleVo.getShowMap());
        }
        this.save(tagRule);
        Long parentId = ruleVo.getParentId();
        String fullId;
        if (Objects.isNull(ruleVo.getParentId())) {
            parentId = 0L;
            fullId = "0,"+ tagRule.getId();
        } else {
            TagRule byId = this.getById(parentId);
            if (Objects.isNull(byId)) {
                throw new ServiceException("父级规则不存在");
            }
            fullId = byId.getFullId() + "," + tagRule.getId();
        }
        tagRule.setFullId(fullId);
        tagRule.setParentId(parentId);
        this.updateById(tagRule);
    }

    @Override
    public void editRule(Long id, TagRuleVo ruleVo) {
        TagRule tagRule = new TagRule();
        tagRule.setId(id);
        tagRule.setRuleName(ruleVo.getRuleName());
        tagRule.setRuleDescribe(ruleVo.getRuleDescribe());
        if (ruleVo.getStatus() != null) {
            tagRule.setStatus(ruleVo.getStatus());
        }
        if (ruleVo.getShowMap() != null) {
            tagRule.setShowMap(ruleVo.getShowMap());
        }
        this.updateById(tagRule);
    }

    @Override
    public List<RuleTreeDto> getRuleTree(RuleQuery query) {
        List<TagRule> rules = this.lambdaQuery().select(TagRule::getId, TagRule::getRuleName).list();
        if(CollectionUtil.isNotEmpty(rules)){
            List<RuleTreeDto> ruleList = this.baseMapper.getRuleList(query);
            ruleList.forEach(x -> {
                Optional<TagRule> first = rules.stream().filter(y -> y.getId().equals((long)x.getParentId())).findFirst();
                first.ifPresent(item -> x.setParentName(item.getRuleName()));
            });
            return TreeUtils.getTree(ruleList,ruleList.stream().map(RuleTreeDto::getId).map(Object::toString).collect(Collectors.toList()));
        }
        return Collections.emptyList();
    }

    @Override
    public List<RuleTreeDto> getRuleOption() {
        List<RuleTreeDto> ruleList = this.baseMapper.getRuleOption();
        return TreeUtils.getTree(ruleList,ruleList.stream().map(RuleTreeDto::getId).map(Object::toString).collect(Collectors.toList()));
    }

    @Override
    public Long getChildrenList(Long id) {
        return this.baseMapper.getChildrenList(id);
    }
}




